<?xml version="1.0" encoding="ISO-8859-1"?>
<!--
    This produces opensearch RSS customized for consumption by PHP MagpieRSS.
    
    Rationale: MagpieRSS flattens channel child elements in such a
    way that the values from multiple dsm:source elements run together.
    eg. given rss containing this
    
      <dsm:sources>
	    <dsm:source id="xxx">
          <dsm:title>XXX</dsm:title>
        </dsm:source>
	    <dsm:source id="yyy">
          <dsm:title>YYY</dsm:title>
        </dsm:source>
      </dsm:sources>
    
    MagpieRSS generates a single value for 
    $rss->channel['dsm']['sources_source_title'] of 'XXXYYY'.
    
    This XSL avoids that by rendering like
    
      <dsm:sources>
	    <dsm:xxx>
          <dsm:title>XXX</dsm:title>
        </dsm:xxx>
	    <dsm:yyy>
          ...
-->
<!DOCTYPE xsl:stylesheet [
  <!ENTITY nbsp  "&#160;">
]>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:encoder="xalan://java.net.URLEncoder"
xmlns:opensearch="http://a9.com/-/spec/opensearch/1.1/"
xmlns:dsm="http://www.educationau.edu.au/"
xmlns:atom="http://www.w3.org/2005/Atom"
exclude-result-prefixes="xsl encoder">

  <xsl:import href="./open_search.xsl"/>
    
  <xsl:template match="/results/sources/source">
    <xsl:element name="{concat('dsm:',@id)}">
      <xsl:apply-templates/>
    </xsl:element>
  </xsl:template>

  <xsl:template name="processMetadataItems">
    <!-- Need to coalesce metadata elements with the same name into a single element
         (with semi-colon separated values) as the magpie RSS reader otherwise will
         concatenate all the values from same name fields into a single value 
         (with no separator).
         Do this by processing metadata elements in groups as described at http://aspalliance.com/34
    -->
    <xsl:variable name="item" select="."/>
    <xsl:variable name="uniqueMetadataNames" select="$item/metadata[not(@name=preceding-sibling::metadata/@name)]/@name" />
    <xsl:for-each select="$uniqueMetadataNames">
      <xsl:sort select="."/>
      <xsl:variable name="metadataElems" select="$item/metadata[@name=current()]"/>
      <xsl:if test="string-length($metadataElems) &gt; 0">
        <xsl:element name="dsm:metadata_{translate(current(),' :','__')}">
          <xsl:value-of select="$metadataElems[1]"/>
          <xsl:for-each select="$metadataElems[position() &gt; 1]">
            <xsl:value-of select="concat('; ', text())"/>
          </xsl:for-each>
        </xsl:element>
      </xsl:if>
    </xsl:for-each>
  </xsl:template>

</xsl:stylesheet>